package zutils

func Camel2Snake(s string) string {
	if len(s) == 0 {
		return ""
	}

	t := make([]byte, 0, len(s) + 4)
	if isASCIIUpper(s[0]) {
		t = append(t, upperLowerExchange(s[0]))
	} else {
		t = append(t, s[0])
	}

	for i := 1; i < len(s); i++ {
		c := s[i]
		if isASCIIUpper(c) {
			t = append(t, '_', upperLowerExchange(c))
		} else {
			t = append(t, c)
		}
	}
	return string(t)
}

func isASCIIUpper(c byte) bool {
	return 'A' <= c && c <= 'Z'
}

func upperLowerExchange(c byte) byte {
	return c ^ ' '
}

func IsEmpty(s string) bool {
	return len(s) <= 0
}